package com.geeguo.ebuilder.client.business.system.permission.service.impl;

import com.geeguo.ebuilder.client.base.core.dictionary.ModuleTypeEnum;
import com.geeguo.ebuilder.client.base.security.model.LoginUser;
import com.geeguo.ebuilder.client.base.tenant.model.DataContext;
import com.geeguo.ebuilder.client.business.system.permission.model.WebMenuMetaVO;
import com.geeguo.ebuilder.client.business.system.permission.model.WebMenuVO;
import com.geeguo.ebuilder.client.business.system.permission.service.PermissionService;
import com.geeguo.ebuilder.client.business.system.role.service.RoleService;
import com.geeguo.ebuilder.client.business.system.webmodule.model.WebModuleBO;
import com.geeguo.ebuilder.client.business.system.webmodule.service.WebModuleService;
import com.geeguo.ebuilder.core.common.dictionary.BooleanEnum;
import com.geeguo.ebuilder.core.common.utils.key.UUIDCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private RoleService roleService;
    @Autowired
    private WebModuleService webModuleService;

    @Override
    public Set<String> listRoleCodeByUser(LoginUser loginUser, DataContext dataContext) {
        return roleService.listCodeByUser(loginUser.getUserId(), dataContext);
    }

    @Override
    public Set<String> listPermissionCodeByUser(LoginUser loginUser, DataContext dataContext) {
        return webModuleService.listPermissionCodeByUser(loginUser, dataContext);
    }

    @Override
    public List<WebMenuVO> listWebMenuByUser(LoginUser loginUser, DataContext dataContext) {
        List<WebMenuVO> menuList = new ArrayList<>();
        List<WebModuleBO> moduleList = webModuleService.listByUser(loginUser, dataContext);
        if (moduleList != null && !moduleList.isEmpty()) {
            Map<String, WebMenuVO> menuMapping = new HashMap<>();
            for (WebModuleBO module : moduleList) {
                if (ModuleTypeEnum.Catalog.getValue() == module.getType() || ModuleTypeEnum.Menu.getValue() == module.getType()) {
                    WebMenuVO menu = new WebMenuVO();
                    menu.setParentId(module.getParentId());
                    menu.setName(module.getName());
                    menu.setPath(module.getPath());
                    menu.setComponent(module.getComponent());

                    WebMenuMetaVO meta = new WebMenuMetaVO();
                    meta.setTitle(module.getTitle());
                    meta.setIcon(module.getIcon());
                    meta.setHidden(BooleanEnum.True.getValue() == module.getHidden());
                    meta.setNoCache(ModuleTypeEnum.Menu.getValue() == module.getType());
                    if (module.getHidden() == BooleanEnum.True.getValue()) {
                        meta.setCanTo(true);
                    }
                    menu.setMeta(meta);

                    if (UUIDCheckUtils.check(module.getParentId()) && menuMapping.containsKey(module.getParentId())) {
                        WebMenuVO parent = menuMapping.get(module.getParentId());
                        parent.addChild(menu);
                        if (module.getHidden() == BooleanEnum.True.getValue()) {
                            meta.setActiveMenu(parent.getMeta().getActiveMenu());
                        } else {
                            meta.setActiveMenu(menu.getName());
                        }
                    } else {
                        menuList.add(menu);
                    }
                    menuMapping.put(module.getId(), menu);
                }
            }
        }
        return menuList;
    }

    @Override
    public void removeCachedUserWebModules(LoginUser loginUser, DataContext dataContext) {
        webModuleService.removeCachedUserWebModules(loginUser, dataContext);
    }
}